package com.tjn.map;

import java.util.*;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class MapFor {
    @SuppressWarnings("all")
    //map的几种遍历方式
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("one",new A("张三",18));
        map.put("two","周二");
        map.put("two","周三");
        map.put("four","周四");
        map.put(null,"周五");
        map.put("six","周六");
        map.put("seven",null);

        //1.通过keySet获取key值，然后通过get方法获取value值
        Set keyset = map.keySet();
        //1.1增强for
        System.out.println("====第一种增强for遍历====");
        for (Object key : keyset) {
            System.out.println(key + "-" + map.get(key));
        }
        //1.2迭代器遍历
        System.out.println("====第一种迭代器遍历====");
        Iterator iterator = keyset.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "-" + map.get(key));
        }

        //2.通过values方法获取value值
        Collection values = map.values();
        //2.1增强for
        System.out.println("====第二种增强for遍历====");
        for (Object value : values) {
            System.out.println(value);
        }
        //2.2迭代器遍历
        System.out.println("====第二种迭代器遍历====");
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object value = iterator1.next();
            System.out.println(value);
        }

        //3.通过entrySet方法获取k-v
        Set entrySet = map.entrySet();
        //3.1增强for
        System.out.println("====第三种增强for遍历====");
        for (Object entry : entrySet) {
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "-" + m.getValue());
        }
        //3.2迭代器遍历
        System.out.println("====第三种迭代器遍历====");
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object entry = iterator2.next();
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "-" + m.getValue());
        }

    }
}
